package com.mijie.homi.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.mina.core.future.ConnectFuture;

import com.mijie.homi.common.constant.TypeCode;
import com.mijie.homi.common.protocol.HomiProtocol;
import com.mijie.util.Helper;

public class Client2 {

	public static void main(String[] args) throws Exception{
//		NioSocketConnector connector = new NioSocketConnector();  
//		connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new HomiProtocolCodecFactory( Charset.forName( "UTF-8" ))));
//		connector.setHandler(new ClientHandler());
//		ConnectFuture cFuture = connector.connect(new InetSocketAddress("192.168.0.166",9997)); 
//		login(cFuture);
//		IoSession session = cFuture.getSession();
//		for(int i=1;i<=10000;i++){
//			HomiProtocol pack = new HomiProtocol(TypeCode.CHAT);
//			JSONObject json = new JSONObject();
//			json.accumulate("category", 1);
//			json.accumulate("receiver", 3);
//			json.accumulate("cid", UUID.randomUUID().toString());
//			json.accumulate("message", "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"+i);
//			json.accumulate("image", "");
//			json.accumulate("length", 100);
//			pack.setContent(json.toString());
//			session.write(pack);
//			Thread.sleep(1000);
//		}
		Class.forName("com.mysql.jdbc.Driver"); 
		String url = "jdbc:mysql://192.168.0.166:3306/homi?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull";
		Connection conn = DriverManager.getConnection(url,"root","woshimijieIAMmijie");
		Statement userStmt = null;
		Statement idboxStmt = null;
		try{
			userStmt = conn.createStatement();
			idboxStmt = conn.createStatement();
			ResultSet rs = userStmt.executeQuery("SELECT uuid,id FROM idBox where id<=10050000 order by id desc limit 50000");
			int uuid = 0;
			int id = 0;
			int gender = 0;
			int counter = 0;
			while(rs.next()){
				uuid = rs.getInt(1);
				id = rs.getInt(2);
				gender = new Random().nextInt(2)+1;
				idboxStmt.execute("delete from idBox where uuid="+uuid);
				idboxStmt.execute("insert into user(id,passwd,email,nickName,gender,createAt) values("+id+",'adcaec3805aa912c0d0b14a81bedb6ff','"+id+"@51homi.com','HAHA"+id+"',"+gender+",now())");
				idboxStmt.execute("insert into userExtInfo(id,gender,createAt) values("+id+","+gender+",now())");
				idboxStmt.execute("insert into terminalInfo(userId,terminalId,deviceId,createAt) values("+id+",'"+UUID.randomUUID().toString()+"','"+UUID.randomUUID().toString()+"',now())");
				idboxStmt.execute("insert into privacy(id,moodLocusVisible) values("+id+",'11')");
				idboxStmt.execute("insert into setting(userId) values("+id+")");
				counter++;
				System.out.println("counter:"+counter);
			}
		}finally{
			if(idboxStmt!=null) idboxStmt.close();
			if(userStmt!=null) userStmt.close();
		}
	}
	
	
	public static void login(ConnectFuture future ){
		HomiProtocol notify = new HomiProtocol(TypeCode.LOGIN);
		JSONObject json = new JSONObject();
		json.accumulate("nickName", "2");
		json.accumulate("passwd", Helper.base64encode(Helper.MD5("23456")));
		json.accumulate("os", "ios");
		json.accumulate("appVer", "1.0.2");
		System.out.println(json);
		notify.setContent(json.toString());
		future.awaitUninterruptibly(1000*5);
		future.getSession().write(notify);
	}
	
	public static void sendMessage(ConnectFuture future,String msg){
		HomiProtocol notify = new HomiProtocol(TypeCode.CHAT);
		JSONObject json = new JSONObject();
		json.accumulate("category", "1");
		json.accumulate("receiver", "3");
		json.accumulate("counter", "1");
		json.accumulate("cid", UUID.randomUUID().toString());
		json.accumulate("message", msg);
		notify.setContent(json.toString());
		future.getSession().write(notify);
		System.out.println("send message->"+msg);
	}
}
